package resource

import (
	"bytes"
	"fcs-api/src/app/utils"
	"fmt"
)

const registerUrl = "http://lcm-scheduler.lcm.svc.cluster.local:9231/lcm/scheduler/register-cluster"
const clusterName = "fcs-cluster"

// server是租户侧集群的管理网ip地址
// name目前写死为"k8s-knative-serving"， 需要和site中的 software/manifests/manifest-knative-serving.yaml
// 文件中的 armada_address 变量匹配
// token是租户侧集群中的 admin token
var lcmRequestBody = `
{
    "cluster":{
        "insecure_skip_tls_verify":true,
        "server":"https://%s:6443"
    },
    "name":"%s",
    "user":{
        "client_certificate_data":"REDACTED",
        "client_key_data":"REDACTED",
        "token":"%s"
    }
}
`

func RegisterCluster(ip, token string) error {
	lcmRequestBody = fmt.Sprintf(lcmRequestBody, ip, clusterName, token)
	result, err := utils.HttpReq(registerUrl, token, "POST", bytes.NewReader([]byte(lcmRequestBody)), nil)
	if err != nil {
		logger.Error("Register lcm error, request body is %s, result is %s, err is %s", lcmRequestBody, string(result), err.Error())
		return err
	}
	return nil
}
